package com.chuck.blogpro.utils;

import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;

import java.util.Date;
import java.util.Map;

public class JWTUtils {

    // 秘钥
    private static final String SALT = "com.chuck.blog";
    // 过期时间
    private static final Long EXPIRE = 43200000L;

    public static String createToken(Map<String, Object> map) {
        return Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, SALT)
                .setClaims(map)
                .setExpiration(new Date(System.currentTimeMillis() + EXPIRE))
                .compact();
    }

    public static Map<String, Object> checkToken(String token) {
        try {
            return Jwts.parser().setSigningKey(SALT).parseClaimsJws(token).getBody();
        } catch (Exception e) {
            return null;
        }
    }
}
